<?php

require_once(dirname(__FILE__).'/../lib/Basesf2s3iGuardActions.class.php');

class sf2s3iGuardActions extends Basesf2s3iGuardActions
{

  public function executeLogin()
  {
    if ($this->getRequest()->getMethod() != sfRequest::POST)
    {

      return sfView::SUCCESS;
    }
    else
    {
      return $this->redirect('@homepage');
    }
  }

  public function handleErrorLogin()
  {
   $this->setFlash('error', 'Erreur d\'identification', false);
   return sfView::SUCCESS;
  }

  public function executeLogout()
  {
    $this->getUser()->signOut();
    $this->redirect('@login');
  }

  public function executeForget()
  {

    $this->sendMail();

    $this->setFlash('notice', 'Un email, avec les instructions pour obtenir un nouveau mot de passe, ' .
        'vient de vous etres envoyé.', false);
    $this->redirect('@login');
  }

  public function handleErrorForget()
  {

    $this->setFlash('error', 'Vous devez fournir une adresse e-mail valide.', false);

    $this->redirect('@login');
    return sfView::SUCCESS;
  }

  public function executeInstructionText()
  {
    $c = new Criteria();
    $c->add(GuardUserPeer::EMAIL, $this->getRequestParameter('email'));
    $this->user = GuardUserPeer::doSelectOne($c);

    $this->user->setGenerateToken();
    $this->user->save();

    $this->setLayout(false);
    return sfView::SUCCESS;
  }

  public function executePasswordText()
  {
    $this->user = $this->getUserByEncodeQ();

    $this->setLayout(false);
    return sfView::SUCCESS;
  }

  public function executeInitPassword()
  {

    $user = $this->getUserByEncodeQ();

    $p = GuardUser::generate_password();

    $this->getUser()->setAttribute('password', $p);
    $user->setPassword($p);
    $user->save();

    $email = $user->getEmail();

    $this->sendMailPassword($email);

    $this->getUser()->getAttributeHolder()->remove('password');

    $this->setFlash('notice', 'Un nouveau mot de passe vient de vous ' .
        'etres envoy&eacute; par email.', false);
    $this->redirect('@login');
  }

  private function getUserByEncodeQ()
  {
    $q = $this->getRequestParameter('q');
    $this->forward404Unless($q);

    list($login, $token) = split('\|', base64_decode($q));

    $c = new Criteria();
    $c->add(GuardUserPeer::LOGIN, $login);
    $user = GuardUserPeer::doSelectOne($c);
    $this->forward404Unless($user);

    return $user;
  }

  private function sendMail()
  {
    // Create our connection, in this case an SMTP connection
    $conn   = new Swift_Connection_SMTP(sfConfig::get('smtp_host'));

    // Need auth for SMTP
    if (sfConfig::get('smtp_user'))
    {
      $conn->setUsername( sfConfig::get('smtp_user') );
      if (sfConfig::get('smtp_pass'))
      {
        $conn->setPassword( sfConfig::get('smtp_pass') );
      }
    }

    $mailer = new sfSwiftPlugin($conn);

    // Get our message bodies
    $body = $this->getPresentationFor('sf2s3iGuard', 'instructionText');

    $message = new Swift_Message(sfConfig::get('site_url_application') . " > Demande d'un nouveau mot de passe");
    $message->attach(new Swift_Message_Part($body));

    // Send out our mailer
    $mailer->send($message, $this->getRequestParameter('email'), sfConfig::get('smtp_sender'));
    $mailer->disconnect();
  }

  private function sendMailPassword($email)
  {
    // Create our connection, in this case an SMTP connection
    $conn   = new Swift_Connection_SMTP(sfConfig::get('smtp_host'));

    // Need auth for SMTP
    if (sfConfig::get('smtp_user'))
    {
      $conn->setUsername( sfConfig::get('smtp_user') );
      if (sfConfig::get('smtp_pass'))
      {
        $conn->setPassword( sfConfig::get('smtp_pass') );
      }
    }

    $mailer = new sfSwiftPlugin($conn);

    // Get our message bodies
    $body = $this->getPresentationFor('sf2s3iGuard', 'passwordText');

    $message = new Swift_Message(sfConfig::get('site_url_application') . " > Votre nouveau mot de passe");
    $message->attach(new Swift_Message_Part($body));

    // Send out our mailer
    $mailer->send($message, $email, sfConfig::get('smtp_sender'));
    $mailer->disconnect();
  }
}
